<?php
abstract class BaseAppConfig extends Config
{
    public $APP_NAME;
    public $APP_PREFIX;
    public $APP_DOMAIN;

    public $DOMAIN_ALIAS = array();

    public $URL_ROUTES = array();

    public $APP_LIVE_MODE   = false;
    public $APP_TEST_MODE   = true;
    public $APP_DEBUG_MODE  = true;
    public $APP_LOCAL_MODE  = true;
    public $APP_MIGRATION_MODE = false;
    public $APP_DOWNLOAD_IMG_MODE = false;

    public $URL_DOWNLOAD_IMG;

    public $CACHE_HTML_MODE  = false;
    public $CACHE_DATA_MODE  = false;

    public $JS_COMBINE_MODE= false;
    public $JS_MINIFY_MODE = true;
    public $JS_PACK_MODE = false;

    public $DEFAULT_COUNTRY;
    public $DEFAULT_LANGUAGE;
    public $DEFAULT_CURRENCY;
    public $DEFAULT_TIMEZONE;
    public $DEFAULT_CONTROLLER = 'Home';

    public $LANGUAGE_TRANSLATE_MODE = false;
    public $LANGUAGE_SERVICE_MODE = false;
    public $LANGUAGE_SERVICE_INSERT_MODE = false;

    public $LOG_EMAIL = false;
    public $LOG_FILE  = true;

    public $DB_INFORMATION_SCHEMA = 'information_schema';

    public $SQL_STRING      = 'string';
    public $SQL_NUMBER      = 'number';
    public $SQL_BOOLEAN     = 'boolean';
    public $SQL_LIST        = 'list';
    public $SQL_STRING_NOQUOTE = 'stringnoquote';
    public $SQL_MIX         = 'mix';

    public $SIGN_DEBUG          = '<!-- debug-info -->';
    public $SIGN_FATAL_ERROR    = '<!-- fatal-error -->';

    protected $REQUEST_APP_DEBUG_MODE   = 'debug_mode';
    protected $REQUEST_CLEAR_IMG        = 'clear_img';
    public $REQUEST_CLEAR_CACHE         = 'clear_cache';

    protected $SESSION_APP_DEBUG_MODE;
    protected $SESSION_CLEARCACHE;

    public $EMAIL_DEBUG;

    public $MCRYPT_KEY;

    protected $REPLACE_VALUES = array();

    public function BaseAppConfig($url = null)
    {
        parent::__construct($url);

        $file = $this->DIR_DATA.DS.'setting.db.php';
        if (file_exists($file)) include($file);

        $this->SESSION_CLEARCACHE       = $this->APP_PREFIX.$this->REQUEST_CLEAR_CACHE;
        $this->SESSION_APP_DEBUG_MODE   = $this->APP_PREFIX.$this->REQUEST_APP_DEBUG_MODE;

        $this->SetRequestSession($this->REQUEST_APP_DEBUG_MODE);
        $this->SetRequestSession($this->REQUEST_CLEAR_CACHE);

        $this->MCRYPT_KEY = 'f82e62d7c3ea69cc12b5cdb8d621dab6';
    }

    public function IsDebugMode()
    {
        return $this->APP_DEBUG_MODE || Session::IsExistedAndTrue($this->SESSION_APP_DEBUG_MODE);
    }

    public function IsClearCache()
    {
        return Session::IsExisted($this->SESSION_CLEARCACHE);
    }

    protected function SetRequestSession($request_name)
    {
        if (isset($_REQUEST[$request_name]))
            Session::Set($this->APP_PREFIX.$request_name, $_REQUEST[$request_name]);
    }

    public function AddReplaceValue($key, $value)
    {
        $this->REPLACE_VALUES[$key] = $value;
    }

    public function GetReplaceValues()
    {
        return array_merge(array(
            'app_domain' => $this->APP_DOMAIN,
            'http_host' => $this->HTTP_HOST,
            'https_host' => $this->HTTPS_HOST,
            'url_app' => $this->URL,
            'url_img' => $this->URL_IMG
        ), $this->REPLACE_VALUES);
    }

    public function GetLanguageId()
    {
        $langId = Session::Get('langId');
        return $langId ? $langId : $this->DEFAULT_LANGUAGE;
    }

    public function SetLanguageId($langId)
    {
        Session::Set('langId', $langId);
    }

    public function GetCurrencyId()
    {
        $currencyId = Session::Get('currencyId');
        return $currencyId ? $currencyId : $this->DEFAULT_CURRENCY;
    }

    public function SetCurrencyId($currencyId)
    {
        Session::Set('currencyId', $currencyId);
    }

    public function GetCountryId()
    {
        $countryId = Session::Get('countryId');
        return $countryId ? $countryId : $this->DEFAULT_COUNTRY;
    }

    public function SetCountryId($countryId)
    {
        Session::Set('countryId', $countryId);
    }

    protected function AddRow($key, $value)
    {
        if ($value && $value == 'true') $value = true;
        else if (!$value || $value == 'false') $value = false;
        $this->$key = $value;
    }
}
?>
